﻿using UnityEngine;
using System.Collections;
using System;
namespace GJXQGame
{
    public class ChessPieseSlot : MonoBehaviour
    {

        public DateModelControl.Point JewelPosition;   // 棋子在棋盘中的位置

        public PowerType JewelPower = PowerType.NONE;          // 棋子特效 也就是特殊功能的棋子 表现方式 

        public TypePlay Type = TypePlay.None;

        private GameObject Slot;
        // Use this for initialization
        void Start()
        {
            UIEventListener.Get(gameObject).onClick += OnClick;
        }



        private void OnClick(GameObject Obj)
        {
            if (GameManager.ChessControl.Pointer == null)
            {
                Debug.LogError(JewelPower);
                if (JewelPower > 0)
                {
                    if (Type == TypePlay.My)
                    {
                        GameManager.ChessControl.Pointer = gameObject;
                    }
                    else
                    {
                        GameManager.ChessControl.ShowTitle("不能操作对方棋子哦!!!");
                    }
                }
            }
            else if (GameManager.ChessControl.Selected == null && gameObject != GameManager.ChessControl.Pointer)
            {
                GameManager.ChessControl.Selected = gameObject;
                GameManager.ChessControl.MoveChess();
            }
        }

        public void RefShow()
        {
            gameObject.transform.localPosition = new Vector3((JewelPosition.Point_x - 4) * 80 + 40, (JewelPosition.Point_y - 4) * 80 + 40,-332);
            if (Slot != null)
            {
                GameObject.Destroy(Slot);
            }
            if (JewelPower == 0)
            {
                gameObject.GetComponent<UISprite>().spriteName = "null";
            }
            else if (JewelPower == PowerType.KING)
            {
                
                Slot =GameObject.Instantiate(Resources.Load<GameObject>("Windows/Prefab/ChessT/"+(Type == TypePlay.My ? "My" : "Other") + "King"));
                SetPosition(Slot);
                //gameObject.GetComponent<UISprite>().spriteName = 
            }
            else if (JewelPower == PowerType.QING)
            {
                Slot = GameObject.Instantiate(Resources.Load<GameObject>("Windows/Prefab/ChessT/" + (Type == TypePlay.My ? "My" : "Other") + "Qing"));
                SetPosition(Slot);
                //gameObject.GetComponent<UISprite>().spriteName = (Type == TypePlay.My ? "My" : "Other") + "Qing";
            }
            else if (JewelPower == PowerType.ROOK)
            {
                Slot =GameObject.Instantiate(Resources.Load<GameObject>("Windows/Prefab/ChessT/"+(Type == TypePlay.My ? "My" : "Other") + "Rook"));
                SetPosition(Slot);
                //gameObject.GetComponent<UISprite>().spriteName = (Type == TypePlay.My ? "My" : "Other") + "Rook";
            }
            else if (JewelPower == PowerType.BISHOP)
            {
                Slot =GameObject.Instantiate(Resources.Load<GameObject>("Windows/Prefab/ChessT/"+(Type == TypePlay.My ? "My" : "Other") + "Bishop"));
                SetPosition(Slot);
                //gameObject.GetComponent<UISprite>().spriteName = (Type == TypePlay.My ? "My" : "Other") + "Bishop";
            }
            else if (JewelPower == PowerType.KNIGHT)
            {
                Slot = GameObject.Instantiate(Resources.Load<GameObject>("Windows/Prefab/ChessT/" + (Type == TypePlay.My ? "My" : "Other") + "Knight"));
                SetPosition(Slot);
                //gameObject.GetComponent<UISprite>().spriteName = (Type == TypePlay.My ? "My" : "Other") + "Knight";
            }
            else if (JewelPower == PowerType.PAWN)
            {
                Slot = GameObject.Instantiate(Resources.Load<GameObject>("Windows/Prefab/ChessT/" + (Type == TypePlay.My ? "My" : "Other") + "Pawn"));
                SetPosition(Slot);
                //gameObject.GetComponent<UISprite>().spriteName = (Type == TypePlay.My ? "My" : "Other") + "Pawn";
            }
            
        }

        public void SetPosition(GameObject Slot)
        {
            Slot.transform.parent = this.transform;
            Slot.transform.localPosition = new Vector3(0, 0, 0);
            Slot.transform.localEulerAngles = new Vector3(-60, 0, 0);
            Slot.transform.localScale = Vector3.one * 55;
        }

        public bool OtherChange()
        {
            GameManager.ChessControl.ShowCover();
            if (JewelPower == PowerType.KING)
            {
                for (int i = 0; i < GameManager.ChessDataControl.ListMyChess.Count; i++)
                {
                    ChessPieseSlot slotTag = GameManager.ChessDataControl.ListMyChess[i].GetComponent<ChessPieseSlot>();
                    if ((slotTag.JewelPosition.Point_x == JewelPosition.Point_x && Math.Abs(slotTag.JewelPosition.Point_y - JewelPosition.Point_y) == 1) || (slotTag.JewelPosition.Point_y == JewelPosition.Point_y && Math.Abs(slotTag.JewelPosition.Point_x - JewelPosition.Point_x) == 1) || (Math.Abs(slotTag.JewelPosition.Point_x - JewelPosition.Point_x) == 1 && Math.Abs(slotTag.JewelPosition.Point_y - JewelPosition.Point_y) == 1))
                    {
                        return ChangeChess(slotTag);
                    }
                }
            }
            else if (JewelPower == PowerType.QING)
            {
                for (int i = 0; i < GameManager.ChessDataControl.ListMyChess.Count; i++)
                {
                    ChessPieseSlot slotTag = GameManager.ChessDataControl.ListMyChess[i].GetComponent<ChessPieseSlot>();
                    if ((slotTag.JewelPosition.Point_x == JewelPosition.Point_x && Math.Abs(slotTag.JewelPosition.Point_y - JewelPosition.Point_y) >= 1) || (slotTag.JewelPosition.Point_y == JewelPosition.Point_y && Math.Abs(slotTag.JewelPosition.Point_x - JewelPosition.Point_x) >= 1) || (Math.Abs(slotTag.JewelPosition.Point_x - JewelPosition.Point_x) == Math.Abs(slotTag.JewelPosition.Point_y - JewelPosition.Point_y)))
                    {
                        return ChangeChess(slotTag);
                    }
                }
            }
            else if (JewelPower == PowerType.ROOK)
            {
                for (int i = 0; i < GameManager.ChessDataControl.ListMyChess.Count; i++)
                {
                    ChessPieseSlot slotTag = GameManager.ChessDataControl.ListMyChess[i].GetComponent<ChessPieseSlot>();
                    if ((slotTag.JewelPosition.Point_x == JewelPosition.Point_x && Math.Abs(slotTag.JewelPosition.Point_y - JewelPosition.Point_y) >= 1) || (slotTag.JewelPosition.Point_y == JewelPosition.Point_y && Math.Abs(slotTag.JewelPosition.Point_x - JewelPosition.Point_x) >= 1))
                    {
                        return ChangeChess(slotTag);
                    }
                }
            }
            else if (JewelPower == PowerType.BISHOP)
            {
                for (int i = 0; i < GameManager.ChessDataControl.ListMyChess.Count; i++)
                {
                    ChessPieseSlot slotTag = GameManager.ChessDataControl.ListMyChess[i].GetComponent<ChessPieseSlot>();
                    if ((Math.Abs(slotTag.JewelPosition.Point_x - JewelPosition.Point_x) == Math.Abs(slotTag.JewelPosition.Point_y - JewelPosition.Point_y)))
                    {
                        return ChangeChess(slotTag);
                    }
                }
            }
            else if (JewelPower == PowerType.KNIGHT)
            {
                for (int i = 0; i < GameManager.ChessDataControl.ListMyChess.Count; i++)
                {
                    ChessPieseSlot slotTag = GameManager.ChessDataControl.ListMyChess[i].GetComponent<ChessPieseSlot>();
                    if ((Math.Abs(slotTag.JewelPosition.Point_y - JewelPosition.Point_y) == 1 && Math.Abs(slotTag.JewelPosition.Point_x - JewelPosition.Point_x) == 2) || (Math.Abs(slotTag.JewelPosition.Point_y - JewelPosition.Point_y) == 2 && Math.Abs(slotTag.JewelPosition.Point_x - JewelPosition.Point_x) == 1))
                    {
                        return ChangeChess(slotTag);
                    }
                }
            }
            else if (JewelPower == PowerType.PAWN)
            {
                for (int i = 0; i < GameManager.ChessDataControl.ListMyChess.Count; i++)
                {
                    ChessPieseSlot slotTag = GameManager.ChessDataControl.ListMyChess[i].GetComponent<ChessPieseSlot>();
                    if (slotTag.JewelPosition.Point_x == JewelPosition.Point_x && (slotTag.JewelPosition.Point_y - JewelPosition.Point_y) == -1)
                    {
                        return ChangeChess(slotTag);
                    }
                }
            }
            return false;
        }

        public bool ChangeChess(ChessPieseSlot slotTag)
        {
            slotTag.GetComponent<ChessPieseSlot>().JewelPower = JewelPower;
            slotTag.GetComponent<ChessPieseSlot>().Type = Type;
            //slotTag.GetComponent<ChessPieseSlot>().RefShow();
            JewelPower = 0;
            Type = TypePlay.None;
            GameObject NewflyObj = GameObject.Instantiate(this.gameObject);
            RefShow();
            GameManager.ChessDataControl.GetMyChess();
            GameRoot.StartCoroutine(WaitTime(slotTag.GetComponent<ChessPieseSlot>(), GameManager.ChessControl.MoveChessTime));
            //NewflyObj.transform.SetParent(this.gameObject.transform.parent);
            NewflyObj.transform.parent = this.gameObject.transform.parent;
            NewflyObj.transform.localPosition = this.gameObject.transform.localPosition;
            NewflyObj.transform.localScale = Vector3.one;
            iTween.MoveTo(NewflyObj, iTween.Hash("position", slotTag.transform.position, "time", GameManager.ChessControl.MoveChessTime-0.05f, "easetype", iTween.EaseType.linear));
            Destroy(NewflyObj, GameManager.ChessControl.MoveChessTime+0.05f);
            return true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tag"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        IEnumerator WaitTime(ChessPieseSlot tag, float time)
        {
            yield return new WaitForSeconds(time);
            tag.RefShow();
            //RefShow();
        }
    }
}
